package role

import (
	"context"
	"go-zero-project/services/user/rpc/internal/mapper"
	"go-zero-project/services/user/rpc/internal/mapper/bo"

	"go-zero-project/services/user/rpc/internal/svc"
	"go-zero-project/services/user/rpc/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type SelectListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRoleSelectListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SelectListLogic {
	return &SelectListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SelectListLogic) RoleSelectList(in *pb.RoleSelectListReq) (*pb.RoleSelectListResp, error) {
	// 1.查询角色信息不分页
	roleArr, err := mapper.NewRoleMapper().SelectList(l.svcCtx.DBEngin, bo.SysRoleBO{RoleNameLike: in.RoleName})
	if err != nil {
		return nil, err
	}
	// 2.组装参数
	var roleSelectArr []*pb.RoleInfoListResp
	if len(roleArr) < 1 {
		return &pb.RoleSelectListResp{RoleArr: roleSelectArr}, nil
	}
	for _, v := range roleArr {
		roleInfoResp := &pb.RoleInfoListResp{
			RoleId: v.RoleId, RoleName: v.RoleName, Remark: v.Remark,
			CreateTime: v.CreateTime.Format("2006-01-02 15:04:05"),
		}
		roleSelectArr = append(roleSelectArr, roleInfoResp)
	}
	return &pb.RoleSelectListResp{RoleArr: roleSelectArr}, nil
}
